<template>
  <view>
    <view class="popup-container" :class="{ 'show-popup': show }" @click="close" @touchmove.prevent>
      <view class="popup-modal" :class="[position]" @click.stop>
        <slot></slot>
      </view>
    </view>
  </view>
</template>

<script lang="ts">
export default {
  name: 'popup',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    position: {
      type: String,
      default: 'popup-center'
    }
  },
  methods: {
    close() {
      this.$emit('update:show', false);
    }
  }
};
</script>

<style scoped lang="scss">
.popup-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  outline: 0;
  z-index: 999;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  visibility: hidden;
  background: rgba(0, 0, 0, 0.4);
  //background: rgba(33, 33, 33, 0.3);
  transition: visibility 0.4s, opacity 0.4s;
  touch-action: none;
  -webkit-overflow-scrolling: none;

  &.show-popup {
    visibility: visible;
    opacity: 1;

    .popup-modal {
      &.popup-center {
        transform: scale(1);
      }

      &.popup-bottom {
        transform: translateY(0);
        padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
      }
    }
  }

  .popup-modal {
    position: relative;
    background: white;
    padding: 40rpx;

    &.popup-center {
      width: 80%;
      max-width: 600px;
      border-radius: 40rpx;
      transform: scale(0.3);
      transition: transform 0.4s cubic-bezier(0.33, 1, 0.68, 1);
      //transition: transform 0.3s ease-in-out;
    }

    &.popup-bottom {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      border-radius: 40rpx 40rpx 0 0;
      transform: translateY(100%);
      //transition: transform 0.3s ease-in-out;
      transition: transform 0.4s cubic-bezier(0.33, 1, 0.68, 1);
    }
  }
}
</style>